// pages/i_addressxq/i_addressxq.js
const app = getApp()
var flag = 1;var save_user_addr;var get_addr_info;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    region: ['请选择', '', ''],
    name:'',
    mobile:'',
    addr:'',
    id:'',
    type:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var id = options.id;
    var type = options.type;
    save_user_addr = app.data.ucenter_url + 'save_user_addr';
    get_addr_info = app.data.index_url + 'get_addr_info';
    if(id == undefined){
      id = '';
    }else{
      wx.request({
        url: get_addr_info,
        data: {
          id:id
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        success (res) {
          console.log(res.data);
          var info = res.data.info;
          var area = info.area;
          area = area.split('/');
          var region = that.data.region;
          region[0] = area[0];
          region[1] = area[1];
          region[2] = area[2];
          that.setData({
            name:info.name,
            mobile:info.mobile,
            addr:info.addr,
            region:region
          })
        }
      })
    }
    that.setData({
      id:id,
      type:type
    })
  },
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },
  nametap:function(e){
    this.setData({
      name:e.detail.value
    })
  },
  mobiletap:function(e){
    this.setData({
      mobile:e.detail.value
    })
  },
  addrtap:function(e){
    this.setData({
      addr:e.detail.value
    })
  },
  submit:function(e){
    var that = this;
    var id = that.data.id;
    var type = that.data.type;
    var aid = wx.getStorageSync('uid');
    if(flag == 1){
      flag = 0;
      var name = that.data.name;
      if(name == ''){
        wx.showModal({
          title: '提示',
          content: '请填写联系人姓名',
          showCancel: false
        })
        flag = 1;
        return false;
      }
      var mobile = that.data.mobile;
      if(mobile == ''){
        wx.showModal({
          title: '提示',
          content: '请填写联系方式',
          showCancel: false
        })
        flag = 1;
        return false;
      }
      var region = that.data.region;
      if(region[0] == '请选择'){
        wx.showModal({
          title: '提示',
          content: '请选择所在地区',
          showCancel: false
        })
        flag = 1;
        return false;
      }
      var addr = that.data.addr;
      if(addr == ''){
        wx.showModal({
          title: '提示',
          content: '请填写详细地址',
          showCancel: false
        })
        flag = 1;
        return false;
      }
      var area = region[0]+'/'+region[1]+'/'+region[2];
      wx.showLoading();
      wx.request({
        url: save_user_addr,
        data: {
          id:id,
          name:name,
          mobile:mobile,
          addr:addr,
          type:type,
          uid:aid,
          area:area
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        success(res) {
          console.log(res.data);
          wx.hideLoading();
          if(res.data.state == 1){
            wx.showModal({
              title: '提示',
              content: '信息提交成功！',
              showCancel: false,
              success:function(e){
                if(type == 0 && id == ''){
                  wx.redirectTo({
                    url: '/pages/i_addrF/i_addrF',
                  })
                }else if(type == 0 && id != ''){
                  app.data.f_id= id;
                  app.data.fname = name;
                  app.data.fmobile = mobile;
                  app.data.fcity = region[1];
                  app.data.farea = area;
                  app.data.faddr = addr;
                  wx.navigateBack({
                    delta: 1,
                  })
                }
                if(type == 1 && id == ''){
                  wx.redirectTo({
                    url: '/pages/i_addrS/i_addrS',
                  })
                }else if(type == 1 && id != ''){
                  app.data.s_id= id;
                  app.data.sname = name;
                  app.data.smobile = mobile;
                  app.data.scity = region[1];
                  app.data.sarea = area;
                  app.data.saddr = addr;
                  wx.navigateBack({
                    delta: 1,
                  })
                }
              }
            })
            flag = 1;
          }else{
            wx.showModal({
              title: '提示',
              content: res.data.msg,
              showCancel: false
            })
            flag = 1;
          }
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})